<?php


namespace App\Http\Controllers\Admin\Video;

use App\Http\Controllers\Admin\BaseAdminController;
use App\Models\Videos;
use App\Models\VideoComments;
use App\Models\Users;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Auth;

/**
 * 店铺控制器
 *
 * @author lxp
 * @package App\Http\Controllers\Shops
 */
class VideosController extends BaseAdminController
{

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 视频审核列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200318
     */
    public function videoshen()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Videos::orderBy($sort, $order)->where('status',0);
        if (request('title')) {
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            } elseif (is_mobile(request('title'))) {
                $query->where('phone', request('title'));
            } else {
                $query->where('username', 'LIKE', "%" . request('title') . "%");
            }
        }
        // 筛选注册时间
        if (request('created_at')) {
            list($begin, $end) = explode(' - ', request('created_at'));
            $query->whereBetween('created_at', [
                date('Y-m-d H:i:s', strtotime($begin)),
                date('Y-m-d H:i:s', strtotime('+1 day', strtotime($end)))
            ]);
        }
        // 取得列表
        $shops = $query->select([
            'id',
            'views',
            'img',
            'title',
            'comments',
            'author',
            'phone',
            'url',
            'status',
            'created_at'
        ])->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $shops->appends(app('request')->all());

        return view('admin.video.videoshen', [
            'videos' => $shops,
        ]);
    }
    /**
     * 视频列表
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200318
     */
    public function index()
    {
        // 处理排序
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = Videos::orderBy($sort, $order)->where('status','!=',0);
        if (request('title')) {
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            } elseif (is_mobile(request('title'))) {
                $query->where('phone', request('title'));
            } else {
                $query->where('username', 'LIKE', "%" . request('title') . "%");
            }
        }
        // 筛选注册时间
        if (request('created_at')) {
            list($begin, $end) = explode(' - ', request('created_at'));
            $query->whereBetween('created_at', [
                date('Y-m-d H:i:s', strtotime($begin)),
                date('Y-m-d H:i:s', strtotime('+1 day', strtotime($end)))
            ]);
        }
        // 取得列表
        $shops = $query->select([
            'id',
            'views',
            'img',
            'title',
            'comments',
            'author',
            'phone',
            'url',
            'status',
            'created_at'
        ])->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $shops->appends(app('request')->all());

        return view('admin.video.videos', [
            'videos' => $shops,
        ]);
    }
    /**
     * 评论列表 
     * @author yh 20200403
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200403
     * 
    **/ 
    public function comment($id){
        $sort = request('sort', 'id');
        $order = request('order', 'desc');

        $query = VideoComments::orderBy($sort, $order);
        if (request('title')) {
            if (request('title')) {
                $query->where('title', 'LIKE', "%" . request('title') . "%");
            } elseif (is_mobile(request('title'))) {
                $query->where('phone', request('title'));
            } else {
                $query->where('username', 'LIKE', "%" . request('title') . "%");
            }
        }
        // 筛选注册时间
        if (request('created_at')) {
            list($begin, $end) = explode(' - ', request('created_at'));
            $query->whereBetween('created_at', [
                date('Y-m-d H:i:s', strtotime($begin)),
                date('Y-m-d H:i:s', strtotime('+1 day', strtotime($end)))
            ]);
        }
        foreach($query as $g){
            $g['video']= Videos::where('id',$g['vedio_id'])->first();
        }
        $shops = $query->paginate(parent::PERPAGE);
        // 将查询参数拼接到分页链接中
        $shops->appends(app('request')->all());

        return view('admin.video.ideos', [
            'videos' => $shops,
        ]);
    }
    /**
     * 添加视频
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20200403
     */
    public function add()
    {
        $cates = Users::get();
        return view('admin.video.videos_form',['cates'=>$cates]);
    }

    /**
     * 编辑用户
     *
     * @param $uid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170111
     */
    public function edit($id)
    {
        // 取得用户信息
        $user = Videos::findOrFail($id);
        $cates = Users::get();
        return view('admin.video.videos_form', [
            'user' => $user,
            'cates'=>$cates
        ]);
    }

    /**
     * 审核店铺
     *
     * @param $uid
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author yh 20170111
     */
    public function shen($id)
    {
        // 取得用户信息
        $user = Videos::findOrFail($id);

        return view('admin.video.videos_shen', [
            'user' => $user
        ]);
    }

    /**
     * 保存用户
     *
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     * @author lxp 20170206
     */
    public function save()
    {

        // 保存用户信息
        DB::transaction(function () {
            $rule = [

            ];
            if (!request('id')) {
                $rule = array_merge([
                    'title' => 'required|unique:videos,title,' . request('id') . ',id',
                    'author_id' => 'required|',
                    'img' => 'required|',
                    'url' => 'required|',
                ], $rule);
            }

            $this->validate(request(), $rule, [], [
                'url' => '视频资源',
                'author_id' => '作者',
                'title' => '标题',
                'img'=>'视频封面'

            ]);
            $user = Videos::findOrNew(request('id'));
            $uinfo = Users::where('uid', request('author_id'))->select('uid', 'nickname','phone', 'avatar')->first();
            $user->avatar =get_file_url($uinfo ->avatar);
            $user->nickname =$uinfo ->nickname;
            $user -> author =$uinfo ->nickname;
            $user -> phone =$uinfo ->phone;
            $user ->title = request('title');
            $user ->author_id = request('author_id');
            $user ->status = 1;
            $user ->img = request('img');
            $user ->url = request('url');
            $user ->save();
        });

        return $this->success(get_session_url('index'));
    }

    /**
     * 删除用户
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author lxp 20160713
     */
    public function delete($id)
    {
        if (request()->ajax() && intval($id)) {
            // 判断用户是否可以被删除
            // 删除用户
            Videos::destroy($id);
            return $this->success();
        }
    }

    /**
     * 审核店铺
     *
     * @param int $uid
     * @return \Illuminate\Http\JsonResponse
     * @author yh 20200322
     */
    public function refund()
    {
        $user = Videos::findOrNew(request('id'));
        $user->status = intval(request('status'));
        $rule = [];
        if (request('status') == 2) {
            $rule = array_merge([
                'refund_msg' => 'required'
            ], $rule);
            $this->validate(request(), $rule, [], [
                'refund_msg' => '审核不通过原因',
            ]);
        }
        $user->refund_msg = request('refund_msg');
        $user->save();
        return $this->success(get_session_url('index'));
    }
}